package com.tencent.wxpay.demo.common;

import com.tencent.wxpay.demo.util.WeixinUtil;
import com.tencent.wxpay.WXPay;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 系统初始化类
 */
@Component
public class SystemInit implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("系统配置初始化开始================================================");
        init();
        System.out.println("系统配置初始化完毕================================================");
    }
    private void init(){
        // 读取配置文件
        Properties properties = new Properties();
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wxpay.properties");
        try {
            properties.load(inputStream);
            WXPay.initSDKConfiguration(properties.getProperty("KEY"),
                    properties.getProperty("APPID"),
                    properties.getProperty("MCHID"),
                    properties.getProperty("CERT_LOCAL_PATH"),
                    properties.getProperty("CERT_PASSWORD")
            );
            System.out.println("初始化成功");
        } catch (IOException e) {
            System.out.println("初始化失败");
            e.printStackTrace();
        }
    }
}
